package com.lxl.bt.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lxl.bt.R;
import com.yanzhenjie.sofia.Sofia;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by lvxinlong
 * on 协议
 **/
public class WebviewActivity extends BaseActivity {


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tvContent)
    WebView webView;
    private Context context;

    public static void intent(Context fromContext,String title,String url){
        Intent intent = new Intent(fromContext,WebviewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("title",title);
        bundle.putString("url",url);
        intent.putExtras(bundle);
        fromContext.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        context = this;

        Sofia.with(this)
                .invasionStatusBar()
                .statusBarLightFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);

        Bundle bundle = getIntent().getExtras();
        String title = bundle.getString("title");
        String url = bundle.getString("url");
        txtToolbarTitle.setText(title);
        rlBack.setVisibility(View.VISIBLE);

        initWebView();
        webView.loadUrl(url);
        System.out.println("loadUrl:"+url);
    }

    private void initWebView() {
        webView.setWebViewClient(new WebViewClient());
        WebChromeClient webChromeClient = new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress >= 95){
                    //hideProgressDialog();
                }
            }
        };
        webView.setWebChromeClient(webChromeClient);
        //设置焦点
        webView.requestFocus();
        WebSettings mWebSettings = webView.getSettings();
        // 支持js

        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 设置自适应屏幕，两者合用
        // 将图片调整到适合webview的大小
        mWebSettings.setUseWideViewPort(true);
        // 缩放至屏幕的大小
        mWebSettings.setLoadWithOverviewMode(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            mWebSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        // 开启 DOM storage API 功能
        //mWebSettings.setDomStorageEnabled(true);
        // 开启 database storage API 功能
        //mWebSettings.setDatabaseEnabled(true);
        //mWebSettings.setAppCacheMaxSize(1024 * 1024 * 8);
        //mWebSettings.setAppCacheEnabled(true);
        //String appCachePath = getCacheDir().getAbsolutePath();
        //mWebSettings.setAppCachePath(appCachePath);

        // 支持缩放，默认为true。是下面那个的前提
        mWebSettings.setSupportZoom(true);
        // 设置内置的缩放控件.若上面是false，则该WebView不可缩放，这个不管设置什么都不能缩放
        //mWebSettings.setBuiltInZoomControls(true);

        mWebSettings.setAllowFileAccess(true);
        mWebSettings.setSavePassword(false);
        mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebSettings.setLoadsImagesAutomatically(true);
        mWebSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
    }

    @OnClick(R.id.rlBack)
    public void onViewClicked() {
        finish();
    }
}
